home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Locaton of an array?
- Date: 24 Jan 1996 08:06:40 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4e5legINNe15@keats.ugrad.cs.ubc.ca>
- References: <4d4iqk$hs3@overload.lbl.gov>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4d4iqk$hs3@overload.lbl.gov>,
- Mikhail Faiguenblat <mfaiguen> wrote:
- >I am sure this has been answered a million times by now, but could someody help
- >me, please?
- >
- >I have an array in my program, and I need to find out the absolute location in
- >memory where this array is located.
- >I have tried to do it this way:
- >
- >#include <stdio.h>
- >
- >int main() {
- > char arr[10];
- > int addr;
- >
- > printf("sizeof(char *) = %d\n", sizeof(char *));
- > printf("sizeof(int) = %d\n", sizeof(int));
- >
- > addr = arr;
- >
- > printf("addr = %d\n", addr);
- >
- > return 0;
- >}
- >
- >And it did not work:
- >
- >sizeof(char *) = 4
- >sizeof(int) = 4
- >addr = 2063810808
- >
- >Obviously, the computer I run it on does not have 2Gb memory, so I must be
- >doing something wrong.
- >Can anybody help me?
-
- I can see you are posting from a SunOS-based reader, thus I know you are using
- a virtual-memory OS.
-
- Yes, in a sense your computer does have 2GB memory; more specifically, it has a
- large _virtual_ address space, not all of which is populated with "present"
- pages.
-
- When you have virtual memory, it is possible for your program to be using large
- addresses.
-
-
- The typical arrangement of a C process on a 32-bit UNIX machine is this.
-
- The lowest addresses are used for code and static data. After this is the
- uninitialized data segment (called BSS). After the BSS is the "break" address.
- Locations above the break address are not present. You can request, via the
- brk() system call, that the OS move the break up or down to add or remove
- virtual memory from your process.
-
- Now, at the opposite end of your memory (which could be 2GB) is the stack,
- which grows downward. The area between the stack and the break is a vast
- expanse of addresses which don't correspond to physical memory, and hence don't
- use any space. The operating system can instantiate memory in this area by
- adding pages to either the stack or the break end, or anywhere in the middle.
-
- The fact that your array appears to be located somewhere in the upper part of
- the 2nd gigabyte is simply suggests that your stack grows down from the 2GB
- limit. You know that the variable is in the stack, because you have declared it
- as an auto.
-
- The HP-UX OS has a scheme whereby virtual memory is divided into 1GB quadrants.
- The first quadrant is for your code. The second is data. The third is for
- mapping shared libraries, and the fourth is for the kernel's private use.
- Linux (x86) has a similar scheme. Lower 3GB are for code and data, upper gig is
- for the kernel.
-
- By the way, each process has its own 4GB address space most of which is
- independent of other processes. The kernel swaps page tables when it switches
- between tasks. (Page tables map virtual memory to physical memory).
-
- --
-
-